from PySide6.QtCore import *
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtWebEngineWidgets import *
import PySide6.QtWebEngineCore as QtWebEngineCore
import sys
import time

class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setWindowTitle('KuGou')
        self.setWindowIcon(QIcon('DM_20230404000441_001.png'))
        self.resize(1080, 640)

        # 标签栏
        self.tabs = QTabWidget()
        self.tabs.setDocumentMode(True)
        self.setCentralWidget(self.tabs)
        
        self.home_page = QWebEngineView()
        self.home_page_index = self.tabs.addTab(self.home_page, 'Home')
        self.home_page.hide()
        self.home_page.page().settings().setAttribute(QtWebEngineCore.QWebEngineSettings.ShowScrollBars, False) # 隐藏滚动条
        # self.home_page.urlChanged.connect(lambda qurl, browser=self.browser: self.renew_urlbar(qurl, self.browser))
        self.home_page.loadFinished.connect(self.set_home)
        self.home_page.load(QUrl('https://www.kugou.com'))
        
        
        self.player_page = QWebEngineView()
        self.player_page_index = self.tabs.addTab(self.player_page, 'Player')
        self.player_page.hide()
        self.player_page.page().settings().setAttribute(QtWebEngineCore.QWebEngineSettings.ShowScrollBars, False) # 隐藏滚动条
        self.player_page.loadFinished.connect(self.set_player)
        self.player_page.load(QUrl('https://www.kugou.com/mixsong/14ywj551.html'))
        

        self.tabs.setCurrentIndex(self.home_page_index)
    
    def set_player(self):
        with open('player.js') as f:
            js_txt = f.read()
            self.player_page.page().runJavaScript(js_txt)
        self.player_page.show()

    def set_home(self):
        with open('home.js') as f_js:
            with open('scrollbarstyle.css') as f_css:
                css_txt = f_css.read()
                js_txt = f_js.read()
                js_txt.replace('%1', css_txt)
                self.home_page.page().runJavaScript(js_txt)
        self.home_page.show()
        print('======')
        

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()

